//
//  BFShShopCarDataController.m
//  BeautyFood
//
//  Created by imac on 2020/4/26.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "BFShShopCarDataController.h"

@implementation BFShShopCarDataController
//购物车数量
+ (void)stoneobs_fun_get_shopcar_count:(void(^)(NSInteger count))handle{
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }

    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/goods/carCount"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumGet
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
//        [SVProgressHUD dismiss];
        NSDictionary * stoneobs_net_model_dic = [responseObject valueForKey:@"data"];
        NSInteger count = [stoneobs_net_model_dic[@"count"] integerValue];
        if (handle) {
            handle(count);
        }
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
//        [SVProgressHUD dismiss];
//        [SVProgressHUD showErrorWithStatus:error.desc];
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
}
+ (void)stoneobs_fun_update_shopcarcount_ifNeed{

    [BFShShopCarDataController stoneobs_fun_get_shopcar_count:^(NSInteger count) {
        UITabBarController * tabbarvc = (id)UIApplication.sharedApplication.delegate.window.rootViewController;
        UIViewController * vc = tabbarvc.childViewControllers[3];
        if (count) {
             [vc.rt_navigationController.tabBarItem setBadgeValue:@(count).description];
        }else{
             [vc.rt_navigationController.tabBarItem setBadgeValue:nil];
        }
       
    }];
}
+ (void)stoneobs_fun_add_toshopcar_with_goods_id:(NSString*)goods_id num:(NSString*)num spec_id:(NSString*)spec_id handle:(STTools_DataController_stoneobs_handle)handle{
//    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    if (goods_id.length) {
        [stoneobs_net_param_dic setObject:goods_id forKey:@"goods_id"];
    }
    if (num.length) {
        [stoneobs_net_param_dic setObject:num forKey:@"num"];
        if (num.intValue == 0) {
            [stoneobs_net_param_dic setObject:@"1" forKey:@"num"];
        }
    }else{
        [stoneobs_net_param_dic setObject:@"1" forKey:@"num"];
    }
    if (spec_id.length) {
        [stoneobs_net_param_dic setObject:spec_id forKey:@"spec_id"];
    }

    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/goods/add"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumPost
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
//        [SVProgressHUD dismiss];
        NSDictionary * stoneobs_net_model_dic = [responseObject valueForKey:@"data"];
        if (handle) {
            handle(nil,YES,stoneobs_net_model_dic);
        }
        [SVProgressHUD showSuccessWithStatus:@"添加购物车成功"];
        [self stoneobs_fun_update_shopcarcount_ifNeed];
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
//        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:error.desc];
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
}
//编辑购物车，数量传0 则删除
+ (void)stoneobs_fun_change_shopcar_with_c_id:(NSString*)c_id num:(NSString*)num  handle:(STTools_DataController_stoneobs_handle)handle{
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    if (c_id.length) {
        [stoneobs_net_param_dic setObject:c_id forKey:@"id"];
    }
    if (num.length) {
        [stoneobs_net_param_dic setObject:num forKey:@"num"];
    }
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/goods/edit"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumPost
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary * stoneobs_net_model_dic = [responseObject valueForKey:@"data"];
        if (handle) {
            handle(nil,YES,stoneobs_net_model_dic);
        }
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:error.desc];
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
}
//删除购物车
+ (void)stoneobs_fun_delete_shopcar_with_c_ids:(NSString*)c_ids handle:(STTools_DataController_stoneobs_handle)handle{
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
     NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
     NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
     if (stoneobs_net_user_id.length) {
         [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
     }
     if (stoneobs_net_user_token.length) {
         [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
     }
     if (c_ids.length) {
         [stoneobs_net_param_dic setObject:c_ids forKey:@"ids"];
     }

     NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/goods"];
     DDLogInfo(@"url:%@",stoneobs_net_net_url);
     DDLogInfo(@"param:%@",stoneobs_net_param_dic);
     [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                  method:STHttpRequestType_stoneobs_enumDelete
                                                              parameters:stoneobs_net_param_dic
                                                                 success:^(NSURLSessionDataTask *operation, id responseObject) {
         [SVProgressHUD dismiss];
         NSDictionary * stoneobs_net_model_dic = [responseObject valueForKey:@"data"];
         if (handle) {
             handle(nil,YES,stoneobs_net_model_dic);
         }
         [self stoneobs_fun_update_shopcarcount_ifNeed];
         // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
         DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
         
         
     } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
         [SVProgressHUD dismiss];
         [SVProgressHUD showErrorWithStatus:error.desc];
         DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                    stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
     }];
}
@end
